<?php defined( '_VALID_MOS' ) or die( include("404.php") );
switch($_POST["hidden"])
{
	case "";
	// khoi dau trang khong co gia tri submit. khong lam zi ca
	break;
	
	/* khoi su ly su kien submit form them nhom logo */
	case "submit_com_logo_grouplogo_add";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_addgrouplogo($_POST["title"], $core_class->_formatdatetime($_POST["date_add"])) <> FALSE){			
				$core_class->_redirect(".?com=com_logo&view=group&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_addgrouplogo($_POST["title"], $core_class->_formatdatetime($_POST["date_add"])) <> FALSE){			
				$core_class->_redirect(".?com=com_logo&view=group&task=add");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		}
	break;
	
	/* khoi su ly su kien submit form them logo */
	case "submit_com_logo_logo_add";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_addlogo($_POST["image_file"], $_POST["link"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("logo", "order_num"), $_POST["groupid"]) <> FALSE){
				$core_class->_redirect(".?com=com_logo&view=logo&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_addlogo($_POST["image_file"], $_POST["link"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("logo", "order_num"), $_POST["groupid"]) <> FALSE){
				$core_class->_redirect(".?com=com_logo&view=logo&task=add");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_logo&view=group&task=view");
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form sua muc logo */
	case "submit_com_logo_logo_edit";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_editlogo($_POST["image_file"], $_POST["link"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["groupid"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_logo&view=group&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_editlogo($_POST["image_file"], $_POST["link"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["groupid"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_logo&view=logo&task=edit&id=".$_POST["Id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_logo&view=group&task=view");
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form sua muc logo */
	case "submit_com_logo_view";
		if($_POST["task"] == "unpublish"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_pulish_and_un_publish_logo("0", $values[$row]) <> FALSE)
				$check = TRUE;
			}
			if($check == TRUE)
			$GLOBALS['msg'] = "";
			else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
		}
		else if($_POST["task"] == "publish"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_pulish_and_un_publish_logo("1", $values[$row]) <> FALSE)
				$check = TRUE;
			}
			if($check == TRUE)
			$GLOBALS['msg'] = "";
			else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
		}
		else if($_POST["task"] == "orderup"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_orderdownlogo($values[$row]) <> FALSE)
				$GLOBALS['msg'] = "";
				else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
			}
		}
		else if($_POST["task"] == "orderdown"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_orderuplogo($values[$row]) <> FALSE)
				$GLOBALS['msg'] = "";
				else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
			}
		}
		else if($_POST["task"] == "remove"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_remove_logo($values[$row]) <> FALSE)
				$check = TRUE;
			}
			if($check == TRUE){}
			else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
		}
	break;
	
	default:
		$core_class->_redirect(".");exit();
	break;
}

class process{

	// ham su ly di chuyen mau tin xuong phia duoi cua logo
	function process_orderdownlogo($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order_num` from logo WHERE Id = $psesid) As `currenOrder`, 
		(SELECT `order_num` from logo WHERE `order_num` > 
		(SELECT `order_num` from logo WHERE Id = $psesid) 
		Order by `order_num` LIMIT 1) As `preOrder`,
		(SELECT Id from logo WHERE `order_num` = 
		(SELECT `order_num` from logo WHERE `order_num` > 
		(SELECT `order_num` from logo WHERE Id = $psesid) 
		Order by `order_num` LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update logo set `order_num` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update logo set `order_num` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}

	// ham su ly di chuyen mau tin len phia tren cua logo
	function process_orderuplogo($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order_num` from logo WHERE `Id` = $psesid) As `currenOrder`, 
		(SELECT `order_num` from logo WHERE `order_num` < 
		(SELECT `order_num` from logo WHERE `Id` = $psesid) 
		Order by `order_num` desc LIMIT 1) As `preOrder`,
		(SELECT `Id` from logo WHERE `order_num` = 
		(SELECT `order_num` from logo WHERE `order_num` < 
		(SELECT `order_num` from logo WHERE `Id` = $psesid) 
		Order by `order_num` desc LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update logo set `order_num` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update logo set `order_num` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly them nhom logo moi
	function process_addgrouplogo($description, $date_add, $added_by){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into group_logo (`description`, `date_add`) VALUES (?,?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ss", $description, $date_add);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	
	// ham su ly them logo moi
	function process_addlogo($file_path, $link, $date_add, $activated, $order_num, $group_logoID){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into logo(`file_path`, `link`, `date_add`, `activated`, `order_num`, `group_logoID`)
				VALUES(?,?,?,?,?,?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssss", $file_path, $link, $date_add, $activated, $order_num, $group_logoID);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	// ham su ly chinh sua muc logo
	function process_editlogo($file_path, $link, $date_add, $activated, $group_logoID, $Id){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE logo SET `file_path` = ?, `link` = ?, `date_add` = ?, `activated` = ?, `group_logoID` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssss", $file_path, $link, $date_add, $activated, $group_logoID, $Id);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	// ham su lay so thu tu lon nhat cho moi mau tin
	function process_getmaxid($table, $column){
		include("../protected/dbconnect.php");
		$sql = "select MAX(`$column`)+1 As `MaxId` from `$table`;";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($MaxId);
		if($cmd->fetch()){
			if($MaxId == 0)	return 1;
			else return $MaxId;
		}
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly su kien publish va khong publish logo
	function process_pulish_and_un_publish_logo($check, $values){
		include("../protected/dbconnect.php");
		if($check == 0)
		$sql = "Update logo Set `activated` = 0 Where `Id` = ?";
		else $sql = "Update logo Set `activated` = 1 Where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly go bo logo
	function process_remove_logo($values){
		include("../protected/dbconnect.php");
		$myprocess = new process;
		$sql = "Delete from `logo` where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
}